
/**
 * @author xiaokui.li
 * 带级联选择功能的树
 */
Ext.ns('Ext.ux.tree');

Ext.ux.tree.CheckTreePanel = Ext.extend(Ext.tree.TreePanel,{
	cascadeSelect:true,//默认级联选择
	initComponent : function(){
		Ext.ux.tree.CheckTreePanel.superclass.initComponent.call(this);
		if(this.cascadeSelect)
		{
			this.on("checkchange", this.cascadeCheck, this);
		}
	},
	//级联选择
	cascadeCheck : function(node, checked)
	{
		var parentNode = node.parentNode;
        if(parentNode !== null) 
	    {   
          this.parentCheck(parentNode,checked);   
	    }
        node.expand();
        node.attributes.checked = checked;     
        node.eachChild(function(child) 
		{     
           child.ui.toggleCheck(checked);    
           child.attributes.checked = checked;     
           child.fireEvent('checkchange', child, checked);
    	});
	},
	//级联选中父节点
	parentCheck : function(node ,checked){
        var checkbox = node.getUI().checkbox;
        if(typeof checkbox == 'undefined')
            return false;
        if(!(checked ^ checkbox.checked))
            return false;
        if(!checked && this.childHasChecked(node))
            return false;
        checkbox.checked = checked;
       node.attributes.checked = checked;
       //node.getUI().checkbox.indeterminate = checked; //半选中状态
        node.getOwnerTree().fireEvent('check', node, checked);
        var parentNode = node.parentNode;
        if( parentNode !== null)
	     {
            this.parentCheck(parentNode,checked);
        }
    },
    //判断是否有子结点被选中
    childHasChecked : function(node)
   	{
       var childNodes = node.childNodes;
   		if(childNodes || childNodes.length>0)
       {
				for(var i=0;i<childNodes.length;i++)
    		{
    			if(childNodes[i].getUI().checkbox.checked)
        		return true;
     		}
       }
        return false;
    }
}); 
Ext.reg('checktreepanel', Ext.ux.tree.CheckTreePanel);